#ifndef __FORT_H_
#define __FORT_H_
#include <ssh/client.h>
#include <event/ssh_epoll_event.h>
#include <libssh/callbacks.h>
/**
 * 堡垒机事件和会话管理
 * @author: gtinlian
 * @email: gtinlian@sina.com
*/

enum ssh_fort_flag {
    PASSWORD_ERROR_NUMBER=0b11,
    CHANNEL_ERROR=0b100,
    PASSWORD_PASS=0b1000,
    ASSET_ERROR=0b10000
};
namespace fort
{
    class SSHFort
    {
    public:
        SSHClient *client;
        SSHClient *asset;
        ssh_event poll_event;
        EpollSSHEvent *evnet;
        ssh_server_callbacks_struct* server_callbacks;
        ssh_channel_callbacks_struct* channel_callbacks;
        int flag;
        SSHFort() : evnet(nullptr), client(nullptr), asset(nullptr), flag(0b0) {}
        bool client_do_epoll()
        {
            return evnet->add_fd(client->get_fd(), EPOLLIN, this);
        }

        bool asset_do_epoll(){
            return evnet->add_fd(asset->get_fd(), EPOLLIN, this);
        }
        ~SSHFort()
        {
            if(client != nullptr ){
                if(evnet != nullptr){
                    evnet->del_fd(client->get_fd(), EPOLLIN, this);
                }
                if(client->channel != nullptr){
                    ssh_remove_channel_callbacks(client->channel, channel_callbacks);
                }
                client->free_poll_event(poll_event);
                delete client;
                client = nullptr;
            }
            if(asset != nullptr ){
                if(evnet != nullptr){
                    evnet->del_fd(asset->get_fd(), EPOLLIN,this);
                }
                asset->free_poll_event(poll_event);
                if(asset->channel != nullptr){
                    ssh_remove_channel_callbacks(asset->channel, channel_callbacks);
                }
                delete asset;
                asset = nullptr;
            }
            if(poll_event != nullptr){
                ssh_event_free(poll_event);
                poll_event = nullptr;
            }
            if(server_callbacks != nullptr){
                delete server_callbacks;
            }
            if(channel_callbacks != nullptr){
                delete channel_callbacks;
            }
        }
    };
}
#endif